package com.info.zhiduoduo.core.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.info.zhiduoduo.common.constants.StatusMsg;
import com.info.zhiduoduo.common.exception.SmartException;
import com.info.zhiduoduo.core.formbean.BankCardElementFormBean;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;



@Slf4j
@Service
public class BankCardElementCertificationService {

	//公用参数
	private static final String URL = "https://api.huajidata.com/bank_234/check";
	private static final String HOME_QUERY_URL = "https://api.huajidata.com/bank_place";
	private static final String APP_ID = "ed950bcf62a465aea53f1bba58bebba4";
	private static final String APP_KEY = "73627eb45ab17e3d2e9cf48f0d3d1c7f";

	public static final String TYPE = "借记卡";

	public JSONObject postBankCardElement(BankCardElementFormBean formBean) {
		String postData="appid="+APP_ID+
				"&timestamp="+ System.currentTimeMillis() +
				"&sign="+ this.sign()+
				"&name="+formBean.getName()+
				"&bankcard="+formBean.getBankcard();
		if (!StringUtils.isEmpty(formBean.getIdCard())){
			postData+="&idcard="+formBean.getIdCard();
		}
		if (!StringUtils.isEmpty(formBean.getMobile())){
			postData+="&mobile="+formBean.getMobile();
		}
		String resultString ="";
		log.info("postData:"+postData);
		try {
			resultString = Jsoup.connect(URL)
					.requestBody(postData)
					.header("Content-type", "application/x-www-form-urlencoded")
					.ignoreContentType(true)
					.post().text();
		}catch (Exception e){
			e.printStackTrace();
		}
		return JSONObject.parseObject(resultString).getJSONObject("data");
	}

	/**
	 * 银行卡归属地查询
	 * @param bankcard 银行卡号
	 * @return
	 * */
	public static JSONObject homeQuery(String bankcard) {
		if (StringUtils.isEmpty(bankcard)){
			throw new SmartException(StatusMsg.BANKCARD_NULL);
		}
		String postData="appid="+APP_ID+
				"&timestamp="+ System.currentTimeMillis() +
				"&sign="+ sign()+
				"&bankcard="+bankcard;
		String resultString ="";
		log.info("postData:"+postData);
		try {
			resultString = Jsoup.connect(HOME_QUERY_URL)
					.requestBody(postData)
					.header("Content-type", "application/x-www-form-urlencoded")
					.ignoreContentType(true)
					.post().text();
		}catch (Exception e){
			e.printStackTrace();
		}
		JSONObject jsonObject = JSONObject.parseObject(resultString);
		if(!"200".equals(jsonObject.getString("code"))){
			throw new SmartException(Integer.parseInt(jsonObject.getString("code")),jsonObject.getString("msg"));
		}
		return jsonObject.getJSONObject("data");
	}

	public static void main(String[] args) {
		BankCardElementCertificationService bankCardElementCertificationService = new BankCardElementCertificationService();
		System.out.println(bankCardElementCertificationService.homeQuery("6226630602760232").toJSONString());
	}
	/**
	 * 返回数字 1核验一致; 2核验不一致; 3无效卡号或卡状态异常，请换卡重试或联系发卡行;
	 * @param formBean
	 * @return
	 */
	public static Integer getBankCardElement(BankCardElementFormBean formBean) {
		String postData="appid="+APP_ID+
				"&timestamp="+ System.currentTimeMillis() +
				"&sign="+ sign()+
				"&name="+formBean.getName()+
				"&bankcard="+formBean.getBankcard();
		if (!StringUtils.isEmpty(formBean.getIdCard())){
			postData+="&idcard="+formBean.getIdCard();
		}
		if (!StringUtils.isEmpty(formBean.getMobile())){
			postData+="&mobile="+formBean.getMobile();
		}
		String resultString ="";
		log.info("postData:"+postData);
		try {
			resultString = Jsoup.connect(URL)
					.requestBody(postData)
					.header("Content-type", "application/x-www-form-urlencoded")
					.ignoreContentType(true)
					.post().text();
			String obj = JSONObject.parseObject(resultString).getJSONObject("data").get("result").toString();
			return Integer.parseInt(obj);
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 签名算法
	 *
	 * @return 签名字符串
	 */
	public static String sign() {
		try {
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append(APP_ID).append(System.currentTimeMillis()).append(APP_KEY);
			String str = stringBuilder.toString();
			log.info("需要加密的字符串: {}", str);
			// md5加密
			// !!记得设置编码，不同的编码加密得到的结果不一样，如果不设置，默认使用系统文件的编码
			String sign = DigestUtils.md5DigestAsHex(str.getBytes("utf-8"));
			log.info("MD5加密之后的签名: {}", sign);
			return sign;
		} catch (Exception e) {
			log.info("字符串加密出错");
			e.printStackTrace();
			return null;
		}

	}

}
